<?php

    class x_users {
    	protected $db_man;
    	protected $find_usr = "select id from x_users where usuari='%s' and password='%s'";
    	protected $change_pwd = "update x_users set password='%s' where usuari='%s'";
    	
    	function x_users($dbm) {
    		$this->db_man = $dbm;
    	}
    	
    	function find_user($u, $p) {
            $r = $this->db_man->do_query(sprintf($this->find_usr, $u, $p));
            return ($r[0]["id"]);
    	}
    		
    	function check_identity($u, $p) {
    		$r = $this->find_user($u, $p);
    		if ($r) {
                return true;
    		} else {
                return false;
    		}
    	}
    	
    	function change_password($usr, $old, $new, $rep) {
    		if ($new != $rep) {
    			return (-1); // New password does not match confirmation
    		}
    		
    		if (!$this->check_identity($usr, $old)) {
    			return (-2); // Old password does not match.
    		}
    		
    		$this->db_man->do_dml(sprintf($this->change_pwd, $new, $usr));
    		return (0); // Ok.
    	}
    }

?>